package com.beadhouse.bed.web.controller;

import java.util.List;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.beadhouse.bed.ao.BedAo;
import com.beadhouse.bed.service.BedManagementService;
import com.beadhouse.bed.service.BedService;
import com.beadhouse.common.dto.JSONResponse;
import com.beadhouse.entity.BedEntity;
import com.beadhouse.entity.BedManageEntity;
import com.github.pagehelper.PageHelper;


@Controller
@RequestMapping("/api/bed")
public class BedManageCountroller {
	@Autowired
	BedService BedService;
	@Autowired
	BedManagementService BedManagementService;
	//养老院新增床位 参数ao 需要name,操作人createId ok
	@GetMapping("/add")
	@ResponseBody
	public JSONResponse addBed(BedAo ao) throws Exception{
		BedEntity e=BedService.addBed(ao);
		
		
		return JSONResponse.ok(e);
	}
	//床位破损，记录在案。逻辑删除 参数ao需要bedId,操作人createId ok
		@GetMapping("/delete")
		@ResponseBody
		public JSONResponse deleteBed(BedAo ao) throws Exception{
			BedService.deleteBed(ao);
			return JSONResponse.ok("ok");
		}
	//修改床位本身信息，例几号床 需要name,操作人createId ok
	@GetMapping("/retreat")
	@ResponseBody
	public JSONResponse modifyBed(BedAo ao) throws Exception{
		    BedService.modifyBed(ao);
			return JSONResponse.ok("ok");
	}
	
	//查看所有床位信息，以便于浏览 出去删除的 ok
		@GetMapping("/selectAll")	
		public ModelAndView selectAll(BedAo ao) throws Exception{
			int total=18;//总数
			int num1=0;//有人数
			int num2=0;//空床数
			int num3=0;//办理退住人数
			int num4=0;//床位破损数
			PageHelper.startPage(1, total);
			List<BedEntity> list=BedService.queryAllBed(ao);
			for(int i=0 ;i<total;i++){
			   if(list.get(i).getBedStatus()) {
				   num1+=1;
			   }
			   if(!list.get(i).getBedStatus()) {
				   num2+=1;
			   }
			   if(list.get(i).getRemark()!=null)
			   { if(list.get(i).getRemark()==0){
				   num4+=1;
			   }
			   if(list.get(i).getRemark()==1){
				   num3+=1;
			   }}
				 }
			ModelAndView mav=new ModelAndView();
			mav.setViewName("bed/bed");
			mav.addObject("data", list);
			mav.addObject("cum", total);
			mav.addObject("num1", num1);
			mav.addObject("num2", num2);
			mav.addObject("num3", num3);
			mav.addObject("num4", num4);
			return mav;
		}
		@GetMapping("/addJsp")
		@RequestMapping("{id}")
		public ModelAndView bedJsp2addJsp(String id) throws Exception{
			ModelAndView mav=new ModelAndView();
			mav.setViewName("bed/add");		
			mav.addObject("data", id);			
			return mav;
		}
		@RequestMapping("/customerJsp")
		public ModelAndView bedJsp2customerJsp(@RequestParam("id") String id) throws Exception{			
			System.out.println(id);
			ModelAndView mav = new ModelAndView();
			mav.setViewName("bed/customer");
			mav.addObject("id", id);		
			return mav;
		}
		
		@RequestMapping("/replaceBedJsp")
		public ModelAndView bedJsp2replaceBedJsp(@RequestParam("id") String id,@PathParam("customerId") String customerId) throws Exception{
			System.out.println(id);
			List<BedEntity> list=BedService.queryAllfreeBed();
			System.out.println(list);
			ModelAndView mav = new ModelAndView();
			mav.setViewName("bed/replaceBed");
			mav.addObject("id", id);
			mav.addObject("customerId",customerId);
			mav.addObject("data",list);
			
			return mav;
		}
	//在无人床位上添加顾客 参数id,createId,customerId
	@GetMapping("/addCustomer")
	public String addCustomer(BedAo ao) throws Exception{
		
		BedService.addBedCustomer(ao);//在bed状态添加改变 ok
		BedManageEntity be=BedManagementService.addCustomerBed(ao);		
		return "forward:/api/bed/selectAll";
	}
	//在有人床位上除去客人 
	@GetMapping("/deleteCustomer")
	@ResponseBody
	public JSONResponse deleteCustomer(BedAo ao) throws Exception{
		BedService.deleteBedCustomer(ao);//在bed状态添加改变 ok
		BedManageEntity be=BedManagementService.deleteCustomerBed(ao);
		return JSONResponse.ok("顾客id: "+be.getCustomerId()+"在"+be.getCreateTime()+"床位记录id: "+be.getId()+"床位id: "+be.getBedId());
	}
	
	//查询顾客住床所有记录 createId,customerId ok
	@GetMapping("/selectCustomerMessage")
	@ResponseBody
	public JSONResponse selectByIdBed(BedAo ao) throws Exception{
		List<BedManageEntity> list=BedManagementService.queryCustomerBed(ao);
		return JSONResponse.ok("顾客信息："+ao.getCustomerId()+"顾客住床信息："+list);
			
	}
	@GetMapping("/selectEmptyBed")
	@ResponseBody
	public JSONResponse selectEmptyBed() throws Exception{
		List<BedEntity> list=BedService.queryAllfreeBed();
		return JSONResponse.ok(list);
			
	}
	@GetMapping("/replaceBed")
	public String CustomerReplaceBed(String newId,String id,String customerId) throws Exception{

		BedAo ao=new BedAo();
		ao.setId(id);
		ao.setCustomerId(customerId);		
		BedService.deleteBedCustomer(ao);
		BedManageEntity b=BedManagementService.deleteCustomerBed(ao);
		ao.setId(newId);
		BedService.addBedCustomer(ao);
		BedManageEntity be=BedManagementService.addCustomerBed(ao);			
		return "forward:/api/bed/selectAll";
	}
	
}
